<?php
namespace Home\Controller;
use Think\Controller;
class NoticeController extends AllowController {

	//查看公告
    public function index(){
    	//实例化
    	$mod = M('notice');	
        
        //获取总条数
        $tot = $mod->where('drafts_id=0')->Count();
        //显示条数
        $nums = 10;
        //实例化分页
        $page = new \Think\Page($tot,$nums);

        //显示数据
    	$list=$mod->where('drafts_id=0')->order('sendtime desc')->limit($page->firstRow,$page->listRows)->select();
        foreach ($list as $key => $value) {
            $time=$value['sendtime'];
            $list["$key"]['sendtime']=date("Y-m-d H:i:s", $time);
        }
       if(empty($_GET['p'])){
                $p = 1;
            }else{
                $p = $_GET['p'];
            }
        //分配数据
        $this->assign('list',$list);
        $this->assign('curr',$p);
        $this->assign('page',ceil($tot/$nums));
        $this->assign('total',$tot);
        $this->assign('pageinfo',$page->show());
        $this->display("Notice/notice");

        // $a = date('Y-m-d H:i:s','1490460511'); //转换时间戳
        // $b = strtotime(date('Y-m-d H:i:s')); //获取时间戳
    }
    

    //查看公告的删除
    public function deletes(){
        $a =I('post.id');
        $mod = M('notice');

        $cc=$mod->delete($a);
        echo $cc;
    }

    //查看公告的信息
    public function edit(){
        $id = I("get.id");
        // var_dump($id);
        // exit;
        $mod = M("notice");
        $arr = array("id"=>$id);
        $list = $mod->where($arr)->find();
        $this->assign("list",$list);
        $this->display("Notice/update");
    }

    public function update(){
        $mod = M('notice');
        if(!$mod->create()){
            $this->error($mod->getError());
        }else{
            if($mod->save()){
                $this->success('修改成功',U('Notice/index'));
            }else{
                $this->error('修改失败');
            }
        }
    }




    //添加发布公告模块
    public function index1(){

    	$names=$_SESSION['name'];

    	$this->assign("names",$names);
    	$this->display('Notice/new');

    }

    //插入草稿箱信息
    public function insert(){
        //实例化

        $mod = M('Notice');
        $data['drafts_id']=$_POST['id'];
        //获取创建时间戳
        $data['sendtime'] = time();
        $data['addresser'] = $_POST['addresser'];
        $data['title'] = $_POST['title'];
        $data['contents'] = $_POST['contents'];

        if($mod->add($data)){
            echo "1";
        }else{
            echo "0";
        }
        // echo $mod->getLastSql();
     }


    //遍历草稿箱的信息
    public function index3(){
        $mod = M('notice');
        $names = $_SESSION['name'];
        //获取总条数
        $tot = $mod->where("drafts_id=1 AND addresser='{$names}'")->count();
        //显示条数
        $nums = 5;
        //实例化分页
        $page = new \Think\Page($tot,$nums);

        $list = $mod->where("drafts_id=1 AND addresser='{$names}'")->limit($page->firstRow,$page->listRows)->select();

        foreach($list as $key=>$value){
            $time = $value['sendtime'];
            $list[$key]['sendtime'] = date('Y-m-d H:i:s',$time);
        }
         if(empty($_GET['p'])){
                $p = 1;
            }else{
                $p = $_GET['p'];
            }
        $this->assign('list',$list);
        $this->assign('curr',$p);
        $this->assign('page',ceil($tot/$nums));
        $this->assign('total',$tot);
        $this->display('Notice/drafts');

    }

    //草稿箱信息的删除
    public function delete(){
        $a =I('get.id');
        $mod = M('notice');

        $cc=$mod->delete($a);
        echo $cc;
    }

    //显示草稿箱数据
    public function edit1(){
        $id = I("get.id");
        // var_dump($id);
        // exit;
        $mod = M("notice");
        $arr = array("id"=>$id);
        $list = $mod->where($arr)->find();
        $this->assign("list",$list);
        $this->display("Notice/drafts_update");
    }

    //修改草稿箱状态值
    public function updates(){
        $mod = M('notice');
        
        //自定义值 0 
        $data['drafts_id'] = $_POST['id'];
        $data['id'] = $_POST['oid'];

        //获取修改时间戳
        $data['sendtime'] = time();

        $data['addresser'] = $_POST['addresser'];
        $data['title'] = $_POST['title'];
        $data['contents'] = $_POST['contents'];
     
        if($mod->save($data)){
            // $this->success('发送成功',U('Notice/index3'));
            echo "1";
        }else{
            echo "0";
        }
        

    }


}